﻿using NovoUSB2XXX;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;

namespace Test_NovoComm_CSConsole
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("C# Test start.");

            #region General APIs
            int ret = NovoComm.Novo_CreateSdk();
            Console.WriteLine(ret);

            NOVO_DEV_CONFIG myDev = new NOVO_DEV_CONFIG();
            myDev.devType = NOVO_DEV_List.CH347;
            myDev.I2C_EN = true;
            myDev.SPI_EN = false;
            myDev.UART_EN = false;
            myDev.GPIO_EN = false;
           
            ret = NovoComm.Novo_InitDev(ref myDev);
            Console.WriteLine("Novo_InitDev result = {0}", ret);

            StringBuilder devName = new StringBuilder();
            NovoComm.Novo_GetDevName(devName);
            Console.WriteLine("Dev Name = {0}", devName);


            ret = NovoComm.Novo_SetDeviceId(2);
            ret = NovoComm.Novo_SetDeviceId(0);
            Console.WriteLine("Novo_InitDev result ={0}", ret);

            ret = NovoComm.Novo_EnumDevice();

            if (NovoComm.Novo_OpenDevice() == 0)
            {
                Console.WriteLine("Open Device success!\n");
                if (NovoComm.Novo_CloseDevice() == 0)
                    Console.WriteLine("Close Device success!\n");
            }
            #endregion General APIs

            #region /* I2C APIs */

            if (NovoComm.Novo_OpenDevice() == 0)
            {
                Console.WriteLine("Open Device success!\n");
            }

            NOVO_IIC_CONFIG i2c_cfg;
            i2c_cfg.ClockSpeedHz = (UInt32)NOVO_IIC_Speed.IIC_100K;
            i2c_cfg.EnablePu = 0;
            ret = NovoComm.Novo_IIC_Init(ref i2c_cfg);
            Console.WriteLine("IIC Init: {0} \n", ret);

            //ret = NovoComm.Novo_IIC_SetIndex(1);
           // Console.WriteLine("Set IIC index: {0} \n", ret);

            //byte[] devAddrList = new byte[128] ;
            //ret = NovoComm.Novo_IIC_ScanDevAddr(devAddrList);
            //Console.WriteLine("Novo_IIC_ScanDevAddr count:{0} \n", ret);

            byte devAddr_Max96712 = 0x40;
            UInt16 testRegAddr = 0x08A3;
            byte testRegData_wr_u8 = 0xE4;
            byte regData_Rd_u8 = 0;
            UInt16 regData_Rd_u16 = 0;
            UInt32 regData_Rd_u32 = 0;
            ret = NovoComm.Novo_IIC_Rd_U16U8(devAddr_Max96712, testRegAddr,ref regData_Rd_u8);
            Console.WriteLine("Read Reg0x{0}: {1} \n", testRegAddr.ToString("X2"), regData_Rd_u8.ToString("X2"));
            ret = NovoComm.Novo_IIC_Wr_U16U8(devAddr_Max96712, testRegAddr, testRegData_wr_u8);
            ret = NovoComm.Novo_IIC_Rd_U16U8(devAddr_Max96712, testRegAddr, ref regData_Rd_u8);
            Console.WriteLine("Read Reg0x{0}: {1} \n", testRegAddr.ToString("X2"), regData_Rd_u8.ToString("X2"));

            ret = NovoComm.Novo_IIC_Wr_U16(devAddr_Max96712, testRegAddr, 0x1122);
            ret = NovoComm.Novo_IIC_Rd_U16(devAddr_Max96712, testRegAddr, ref regData_Rd_u16);
            Console.WriteLine("Read Reg0x{0}: {1} \n", testRegAddr.ToString("X4"), regData_Rd_u16.ToString("X4"));

            ret = NovoComm.Novo_IIC_Wr_U16U32(devAddr_Max96712, testRegAddr, 0x11223344);
            ret = NovoComm.Novo_IIC_Rd_U16U32(devAddr_Max96712, testRegAddr, ref regData_Rd_u32);
            Console.WriteLine("Read Reg0x{0}: {1} \n", testRegAddr.ToString("X4"), regData_Rd_u32.ToString("X4"));

            if (NovoComm.Novo_CloseDevice() == 0)
                Console.WriteLine("Close Device success!\n");

            #endregion IIC

            #region GPIO
            if (NovoComm.Novo_OpenDevice() == 0)
            {
                Console.WriteLine("Open Device success!\n");
            }

            byte gpio_data = 0;
            byte gpio_ix = 5;
            if (NovoComm.Novo_GPIO_Get((byte)(0x01 << gpio_ix), ref gpio_data) == 0)
                Console.WriteLine("Get GPIO_{0}: {1}", gpio_ix, gpio_data);

            byte gpio_set = 0x00;
            if (NovoComm.Novo_GPIO_Set((byte)(0x01 << gpio_ix), gpio_set) == 0)
                Console.WriteLine("Set GPIO_{0}: {1}", gpio_ix, gpio_set);

            if (NovoComm.Novo_GPIO_Get((byte)(0x01 << gpio_ix), ref gpio_data) == 0)
                Console.WriteLine("Get GPIO_{0}: {1}", gpio_ix, gpio_data);

            if (NovoComm.Novo_CloseDevice() == 0)
                Console.WriteLine("Close Device success!\n");

            #endregion GPIOs

        }

        
    }
}
